package ro.bluecube.admlocal.gui.users;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

import ro.bluecube.admlocal.gui.commons.AbstractForm;
import ro.bluecube.admlocal.gui.commons.FormEvent;
import ro.bluecube.admlocal.gui.commons.Utils;
import ro.bluecube.admlocal.main.MainFrame;
import ro.bluecube.admlocal.persons.entity.Person;
import ro.bluecube.admlocal.users.entity.User;

/**
 * @author anton
 *
 */
public class UsersForm extends AbstractForm {

	public UsersForm() {
		super();
	}

	private static final long serialVersionUID = 1L;
	private JTable usersTable;
	private UsersTableModel usersTableModel;
	
	private User user;
	
	private JButton butAdaugare;
	private JButton butModificare;
	private JButton butStergere;

	private JDialog updateUser;
	private JComboBox comboPerson;
	private ComboBoxModel personsComboModel;
	private JTextField textUserName;
	private JPasswordField textPassword;
	
	private JButton buttonOk;
	private JButton buttonCancel;
	
	public void initComponents() {
		setLayout(new GridBagLayout());
		usersTableModel = new UsersTableModel(new ArrayList<User>());
		usersTable = new JTable(usersTableModel);
		usersTable.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				int row = usersTable.getSelectedRow();
				if (row >= 0){
					butModificare.setEnabled(true);
					butStergere.setEnabled(true);
				} else{
					butModificare.setEnabled(false);
					butStergere.setEnabled(false);
				}
			}
		});
		usersTable.setAutoCreateColumnsFromModel(false);
		usersTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		for (int k = 0; k < usersTableModel.getColumnCount(); k++) {
			DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
			renderer.setHorizontalAlignment(
			JLabel.CENTER);
			TableColumn column = usersTable.getColumn(usersTableModel.getColumnName(k));
			column.setCellRenderer(renderer);
			column.setCellEditor(null);
		}
		JScrollPane jsp = new JScrollPane(usersTable);
		jsp.setBorder(new TitledBorder("users"));		
		
		add(jsp,new GridBagConstraints(0,0,1,1,3,1,GridBagConstraints.EAST,GridBagConstraints.BOTH,new Insets(1,1,1,1),0,0));
		populate();
	}

	public void populate() {
		
	}

	public JPanel getToolBar() {
		JPanel panel = new JPanel(new GridBagLayout());
		Dimension dim = new Dimension(100,26);
		butAdaugare = new JButton("Adauga");
		butAdaugare.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				handleEvent(new FormEvent(this,UsersController.ACTION_ADAUGARE,"adauga"));
				updateUser.setVisible(true);
			}
		});
		butAdaugare.setMinimumSize(dim);
		butAdaugare.setPreferredSize(dim);
		butModificare = new JButton("Modifica");
		butModificare.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				handleEvent(new FormEvent(this,UsersController.ACTION_MODIFICARE,"modifica"));
			}
		});
		butModificare.setMinimumSize(dim);
		butModificare.setPreferredSize(dim);
		butModificare.setEnabled(false);
		butStergere = new JButton("Sterge");
		butStergere.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				handleEvent(new FormEvent(this,UsersController.ACTION_STERGERE,"sterge"));
			}
		});
		butStergere.setMinimumSize(dim);
		butStergere.setPreferredSize(dim);
		butStergere.setEnabled(false);
		panel.add(butAdaugare,new GridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.BOTH,new Insets(0,2,0,0),0,0));
		panel.add(butModificare,new GridBagConstraints(0,1,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.BOTH,new Insets(0,2,0,0),0,0));
		panel.add(butStergere,new GridBagConstraints(0,2,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.BOTH,new Insets(0,2,0,0),0,0));
		panel.add(Box.createHorizontalBox(), new GridBagConstraints(0,
				3, 1, 1, 10.0, 10.0, GridBagConstraints.NORTHWEST,
				GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		return panel;
	}
	
	public void setUsers(Collection<User> users){
		usersTableModel.replaceDatas((ArrayList<User>) users);
		usersTable.revalidate();
		usersTable.repaint();
	}

	public User getSelectedArbitru(){
			return usersTableModel.getUser(usersTable.getSelectedRow());
	}
	
	public void initUpdateUserDialog(){
		if (updateUser!=null) return;
		updateUser = new JDialog(MainFrame.getFrame(), true);
		Utils.centerDialogInParent(updateUser, MainFrame.getGeneralPanel());
		personsComboModel = new DefaultComboBoxModel();
		comboPerson = new JComboBox(personsComboModel);
		textUserName = new JTextField();
		textPassword = new JPasswordField();
		
		JLabel labelUserName = new JLabel("Utilizator:");
		JLabel labelPerson = new JLabel("Persoana:");
		JLabel labelPassword = new JLabel("Parola:");
		
		buttonOk = new JButton("Accepta");
		buttonCancel = new JButton("Renunta");
		
		labelUserName.setPreferredSize(new Dimension(200,20));
		textUserName.setPreferredSize(new Dimension(200,20));
		
		comboPerson.setPreferredSize(new Dimension(200,20));
		buttonOk.setPreferredSize(new Dimension(100,20));
		
		buttonOk.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				handleEvent(new FormEvent(this,UsersController.PERFORM_ADAUGARE,"perform adaugare"));
			}
		});
		
		buttonCancel.setPreferredSize(new Dimension(100,20));
		
		JPanel contentPanel = (JPanel) updateUser.getRootPane().getContentPane();
		contentPanel.setLayout(new GridBagLayout());
		
		contentPanel.add(labelUserName, new GridBagConstraints(0, 0, 1, 1, 0,
				0, GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(1, 1, 1, 1), 0, 0));
		contentPanel.add(textUserName, new GridBagConstraints(1, 0, 2, 1, 0,
				0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		
		contentPanel.add(labelPerson, new GridBagConstraints(0, 1, 1, 1, 0,
				0, GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(1, 1, 1, 1), 0, 0));
		contentPanel.add(comboPerson, new GridBagConstraints(1, 1, 2, 1, 0,
				0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		
		contentPanel.add(labelPassword, new GridBagConstraints(0, 2, 1, 1, 0,
				0, GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(1, 1, 1, 1), 0, 0));
		contentPanel.add(textPassword, new GridBagConstraints(1, 2, 2, 1, 0,
				0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		
		contentPanel.add(Box.createHorizontalBox(), new GridBagConstraints(0, 3, 1, 1, 1,
				1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		contentPanel.add(buttonOk, new GridBagConstraints(1, 3, 1, 1, 0,
				0, GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(1, 1, 1, 1), 0, 0));
		contentPanel.add(buttonCancel, new GridBagConstraints(2, 3, 1, 1, 0,
				0, GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(1, 1, 1, 1), 0, 0));
		updateUser.pack();
	}

	public void setPersons(Collection<Person> persons) {
		comboPerson.setModel(new DefaultComboBoxModel(new Vector<Person>(persons)));
	}
	public User getUpdatedUser(){
		if (user==null){
			user = new User();
		}
		user.setPerson((Person) comboPerson.getSelectedItem());
		user.setUserName(textUserName.getText().trim());
		//FIXME getpassword returneaza un char[]. afla de ce
		user.setPassword(new String(textPassword.getPassword()));
		return user;
	}
}